﻿using System.Collections.Generic;
using UnityEngine;
namespace FastDev
{
    public class UIManager : MonoSingleton<UIManager>, IUIManager
    {
        private string bundleName = "ui";
        public Dictionary<string, UIPanel> Panels { get; private set; } = new Dictionary<string, UIPanel>();

        public UIPanel GetPanel(string path)
        {
            if (Panels.ContainsKey(path))
            {
                return Panels[path];
            }
            return null;
        }

        public UIPanel LoadPanel(string path)
        {
            UIPanel uIPanel = GetPanel(path);
            if (uIPanel == null)
            {
                GameObject panelAsset = ResManager.Instance.LoadAsset<GameObject>(bundleName, path);

                if (panelAsset != null)
                {
                    GameObject panelObj = Instantiate(panelAsset, transform);
                    uIPanel = panelObj.GetComponent<UIPanel>();
                    uIPanel.OnLoad();
                    Panels[path] = uIPanel;
                }
            }
            return uIPanel;
        }

        public void RemovePanel(string path)
        {
            if (Panels.ContainsKey(path))
            {
                Destroy(Panels[path].gameObject);
                Panels.Remove(path);
            }
        }
    }
}